<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>黑名单添加</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script type="text/javascript">
        layui.use(['layer', 'form', 'jquery', 'laydate','upload','element'], function () {
            var layer = layui.layer;
            var form = layui.form;
            var $ = layui.$;
            var laydate = layui.laydate;
            var upload = layui.upload;
            var importNum = "";//批量导入号码
            laydate.render({
                elem: '#enterTime'
                , type: 'datetime'
                , value: new Date()
                , btns: ['confirm']
            });
            laydate.render({
	             elem: '#ComplaintTime'
	             , type: 'datetime'
	             ,max:maxDate()
	        });
	        
	        function maxDate(){
			    var now = new Date();
			    return now.getFullYear()+"-" + (now.getMonth()+1) + "-" + now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
			}
			
			upload.render({
                elem: '#import'
                , url: '/blackUser/upload'
                , auto: false //关闭自动上传
                , accept: 'file' //普通文件
                , exts: 'csv' //只允许上传csv文件
                //,multiple: true
                , bindAction: '#upload'
                , before: function (obj) {
                    layer.load(); //上传loading
                }
                , done: function (obj) {
                    layer.closeAll('loading'); //关闭loading
                    importNum="";
                    $("#importNum").val(importNum);
                    if (obj.code == 0) {
                        var data=obj.data;
                        // var num= data.toString().split(",");
                        layer.open({
                            title: '导入成功'
                            , content: '成功导入' + data.length + '个电话号码'
                        });
                        for(var i=0;i<data.length;i++){
                        	importNum+=data[i]+",";
                        }
                        importNum=importNum.substring(0,importNum.length-1);
                        $("#importNum").val(importNum);
                        $(".importNum").css("display","");
                    }
                    if (obj.code > 0) {
                        layer.msg(obj.msg);
                    }

                }
                , error: function (index, upload) {
                    layer.closeAll('loading'); //关闭loading
                    layer.msg("上传过程中出现异常，请重试");
                }

            });
			
			//根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //根据城市ID或区县ID和项目简称查询项目
            function selectMgrInfo(countyId,mgrName) {
                var data;
                var url = '/util/findByMgrName?countyId=' + countyId +'&mgrName='+mgrName;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //监听项目
            form.on('select(countyId)', function (data) {
            	var mgrName = $("#mgrName").val();
            	var cityId = $("#cityId").val();
            	if(data.value==null||data.value==''){
            		var data1 = selectMgrInfo(cityId,mgrName);
            		appendMgr(data1);
            	}else{
            		var data1 = selectMgrInfo(data.value,mgrName);
                	appendMgr(data1);
            	}
                
            });
            
			//监听区县
            form.on('select(cityId)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var mgrName = $("#mgrName").val();
                var data1 = selectMgrInfo(data.value,mgrName);
                appendMgr(data1);
            });
			//动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">全部</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            //动态拼接项目
            function appendMgr(data) {
            		if (data.length >0) {
                        var checkboxs = '';
                        for (var i = 0; i < data.length; i++) {
                            checkboxs += '<input type="checkbox" value="' + data[i].id + '" name="mgrId"  title="' + data[i].name + '" />';
                        }
                        $('#checkbox').html(checkboxs);
                    } else {
                        var checkboxs = '<span>该区域下没有项目</span>';
                        $('#checkbox').html(checkboxs);
                    }
                	form.render();
            }
            $('#submit').on('click', function () {
            	var phoneNumberChoose = $("input[name='phoneNumber']:checked").val();
            	var phoneNumber;
            	if(phoneNumberChoose==1){
            		phoneNumber = $("#phoneNumber").val();
            	}else if(phoneNumberChoose==2){
            		phoneNumber = $("#importNum").val();
            	}
                var cityId = $("#cityId").val();
                var countyId = $("#countyId").val();
                var blackLevel = $("input[name='blackLevel']:checked").val();
                var enterTime = $("#ComplaintTime").val();
                var mgrId = "";
                $("input:checkbox[name='mgrId']:checked").each(function () {
                    mgrId += $(this).val()+"+";
                });
                if (mgrId!=null&&mgrId!='') {
                	mgrId=mgrId.substring(0,mgrId.length-1);
                }
                var param = {
                    phoneNumber: phoneNumber,
                    cityId: cityId,
                    //countyId: countyId,
                    mgrId: mgrId,
                    blackLevel: blackLevel,
                    enterTime: enterTime
                };
                var jsonParams=JSON.stringify(param);
                var url = '/blackUser/add';
                $.ajax({
                    type: 'post',
                    data: param,
                    url: url,
                    beforeSend: function () {
                        if (testMsisdn(phoneNumber)&&phoneNumberChoose==1) {
                            layer.msg("不满足手机号格式");
                            return false;
                        }
                        if (blackLevel=="2"&&isNull(cityId)) {
                            layer.msg("必须选择一个地市");
                            return false;
                        }
                        if (blackLevel=="3"&&isNull(countyId)) {
                            layer.msg("必须选择一个区县");
                            return false;
                        }
                        if (blackLevel=="4"&&isNull(mgrId)) {
                            layer.msg("必须选择至少一个项目");
                            return false;
                        }
                        loading = layer.load(0, {shade: false});
                    },
                    success: function (json) {
                        layer.close(loading);
                        if (json.data == 1) {
	                        layer.confirm('添加成功', {
			                    btn: ['确定'] //按钮
			                }, function () {
			                    location.href="blackManage.html";
			                });
                        } else if (json.data == -1){
                            layer.msg('该手机号已经存在');
                        }else {
                            layer.msg('添加失败，请重试');
                        }
                    },
                    complete: function () {

                    },
                    error: function (data) {

                    }
                });
            });
            
            //身份识别
            $(document).ready(function () {
                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        user=json;
                        if (json.duty == 1) {//地市
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            $("input[name='blackLevel'][value= '1' ]").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            var data1 = selectMgrInfo(json.cityId);
                            appendMgr(data1);
                            form.render();
                        } else if (json.duty == 2) {//项目
                            //var data = selectMgrInfo(json.countyId);
                            appendMgr(json.list);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                        }else if (json.duty == 3) {//区县
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data = selectMgrInfo(json.countyId);
                            appendMgr(data);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                        }else if(json.duty == 4){
                            if(json.cityId==25){//省权限
                            
                            }else if(json.cityId!=25&&checkStr(json.countyId)==""){//地市权限
	                            $("#cityId").val(json.cityId);
	                            $("#cityId").attr("disabled", true);
	                            var data2 = selectCountyId(json.cityId);
	                            appendCounty(data2);
	                            var data1 = selectMgrInfo(json.cityId);
	                            appendMgr(data1);
	                            form.render();
                            }else if(json.cityId!=25&&checkStr(json.countyId)!=""){//区县权限
	                            $("#cityId").val(json.cityId);
	                            $("#cityId").attr("disabled", true);
	                            var data = selectMgrInfo(json.countyId);
	                            appendMgr(data);
	                            var data2 = selectCountyId(json.cityId);
	                            appendCounty(data2);
	                            $("#countyId").val(json.countyId);
	                            $("#countyId").attr("disabled", true);
	                            form.render();
                            }
                        } 
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
                
                $(".blackLevel").click(function(){
                	var blackLevel = $("input[name='blackLevel']:checked").val();
                	if(blackLevel=="1"){
                		$(".province").css("display","none");
                		$(".cityId").css("display","none");
                		$(".countyId").css("display","none");
                		$(".mgrId").css("display","none");
                		$(".mgrName").css("display","none");
                		//$(".checkbox").css("display","none");
                		$("#cityId").val("10");
                		$("#countyId").val("");
                		$("#mgrName").val("");
                		var select = "dd[lay-value='']";
						$('#cityId').siblings("div.layui-form-select").find('dl').find(select).click();
						$('#countyId').siblings("div.layui-form-select").find('dl').find(select).click();
                	}else if(blackLevel=="2"){
                		$(".province").css("display","");
                		$(".cityId").css("display","");
                		$(".countyId").css("display","none");
                		$(".mgrId").css("display","none");
                		$(".mgrName").css("display","none");
                		$("#countyId").val("");
                		$("#mgrName").val("");
                		var select = "dd[lay-value='']";
						$('#countyId').siblings("div.layui-form-select").find('dl').find(select).click();
                	}else if(blackLevel=="3"){
                		$(".province").css("display","");
                		$(".cityId").css("display","");
                		$(".countyId").css("display","");
                		$(".mgrId").css("display","none");
                		$(".mgrName").css("display","none");
                		$("#mgrName").val("");
                		var countyId = $("#countyId").val();
                		var select = "dd[lay-value="+countyId+"]";
						$('#countyId').siblings("div.layui-form-select").find('dl').find(select).click();
                	}else if(blackLevel=="4"){
                		$(".province").css("display","");
                		$(".cityId").css("display","");
                		$(".countyId").css("display","");
                		$(".mgrId").css("display","");
                		$(".mgrName").css("display","");
                	}
                });
                /* $(".import").css("display","none"); */
                 $(".phone").click(function(){
                 	var phoneNumber = $("input[name='phoneNumber']:checked").val();
                 	if(phoneNumber==1){
                 		$(".import").css("display","none");
                 		$(".phoneNumber").css("display","");
                 		$(".importNum").css("display","none");
                 	}else if(phoneNumber==2){
                 		$(".import").css("display","");
                 		$(".phoneNumber").css("display","none");
                 		if(null!=importNum && ""!=importNum){
                 			$(".importNum").css("display","");
                 		}
                 	}
                 });
                 var cityId = $("#cityId").val();
                 var data2 = selectCountyId(cityId);
                 appendCounty(data2);
                 var mgrName = $("#mgrName").val();
                 var data1 = selectMgrInfo(cityId,mgrName);
                 appendMgr(data1);
                 
                 $('#mgrName').bind('input propertychange', function(){
                 		var cityId = $("#cityId").val();
	                 	var countyId = $("#countyId").val();
	                 	if(countyId!=null&&countyId!=''){
	                 		var data1 = selectMgrInfo(countyId,$(this).val());
	                 		appendMgr(data1);
	                 	}else{
	                 		var data1 = selectMgrInfo(cityId,$(this).val());
	                 		appendMgr(data1);
	                 	}
				});
                
            });
        });

    </script>
</head>
<body>
<div class="layui-content-body">
    <div class="scroll-box">
        <div class="breadcrumb-box">
            <span class="layui-breadcrumb" lay-separator="|">
  <a href="javascript:returnIndex2()">首页&nbsp;</a>
  <a href="">黑名单管理</a>
  <a href="blackManage.html"> 黑名单管理</a>
  <a href="blackAdd.html"> 黑名单添加</a>
</span>
        </div>
<div class="content-box">
    <div class="form-select-box">
        <div class="layui-form layui-form-pane">
        	<div class="layui-form-item blackLevel">
	                    <label class="layui-form-label" style="width:120px">黑名单级别</label>
	                        <div class="layui-input-block">
	                            <input class="radioItem" lay-filter="gis" type="radio" name="blackLevel" title="省级" onclick="ChooseRadio(this)" value="1">
	                            <input class="radioItem" lay-filter="gis" type="radio" name="blackLevel" title="地市级" onclick="ChooseRadio(this)" value="2">
	                            <input class="radioItem" lay-filter="gis" type="radio" name="blackLevel" title="区县级" onclick="ChooseRadio(this)" value="3">
	                            <input class="radioItem" lay-filter="gis" type="radio" name="blackLevel" title="项目级" onclick="ChooseRadio(this)" value="4" checked="checked">
	                        </div>
	        </div>
	        <div class="layui-form-item">
		        <div class="layui-inline province">
	                            <label class="layui-form-label">省份</label>
	                            <div class="layui-input-block" style="width:120px;">
	                                <input type="text" name="province" lay-verify="required" value="江苏省"
	                                       autocomplete="off" class="layui-input" disabled>
	                            </div>
	            </div> 
	            <div class="layui-inline cityId">
	                            <label class="layui-form-label">地市</label>
	                            <div class="layui-input-block" style="width:120px;">
	                                <select name="cityId" lay-filter="cityId" id="cityId">
	                                    <option value="10">全部</option>
	                                    <option value="11">苏州</option>
	                                    <option value="12">淮安</option>
	                                    <option value="13">宿迁</option>
	                                    <option value="14">南京</option>
	                                    <option value="15">连云港</option>
	                                    <option value="16">徐州</option>
	                                    <option value="17">常州</option>
	                                    <option value="18">镇江</option>
	                                    <option value="19">无锡</option>
	                                    <option value="20">南通</option>
	                                    <option value="21">泰州</option>
	                                    <option value="22">盐城</option>
	                                    <option value="23">扬州</option>
	                                    <option value="25">省级</option>
	                                </select>
	                            </div>
	             </div>
				 <div class="layui-inline countyId">
	                            <label class="layui-form-label">区县</label>
	                            <div class="layui-input-block" style="width:150px">
	                                <select name="countyId" lay-filter="countyId" id="countyId">
	                                </select>
	                            </div>
	            </div>
	            <div class="layui-inline mgrName">
	                            <label class="layui-form-label">项目简称</label>
	                            <div class="layui-input-block" style="width:150px">
	                                <input type="text" name="mgrName" id="mgrName" lay-verify="required" placeholder="请输入项目简称"
	                                            autocomplete="off" class="layui-input" />
	                            </div>
	            </div>
        </div>
        <div class="layui-form-item checkbox">
                        <div class="layui-inline">
                            <label class="layui-form-label">项目</label>
                            <div class="layui-input-block" id="checkbox">
                            </div>
                        </div>
        </div>
        <div class="layui-form-item ComplaintTime">
                            <label class="layui-form-label">投诉时间</label>
                            <div class="layui-input-block" style="width:150px">
                                <input type="text"  name="date" id="ComplaintTime" lay-verify="required" placeholder="请输入"
                                            autocomplete="off" class="layui-input" />
                            </div>
	    </div>
	    <div class="layui-form-item phone">
	                    <label class="layui-form-label" style="width: auto;">&nbsp;&nbsp;&nbsp;投诉电话&nbsp;&nbsp;</label>
	                        <div class="layui-input-block">
	                            <input class="radioItem" lay-filter="gis" type="radio" name="phoneNumber" title="单个录入" onclick="ChooseRadio(this)" value="1" checked="checked">
	                            <input class="radioItem" lay-filter="gis" type="radio" name="phoneNumber" title="批量导入" onclick="ChooseRadio(this)" value="2">
	                        </div>
	    </div>
	    <div class="layui-form-item phoneNumber">
	    				<label class="layui-form-label" style="width: auto;opacity: 0;">&nbsp;&nbsp;&nbsp;投诉电话&nbsp;&nbsp;</label>
	                    <div class="layui-input-block" style="width: 250px;">
	                                <input type="text" name="phoneNumber" id="phoneNumber" lay-verify="required" placeholder="请输入号码"
	                                            autocomplete="off" class="layui-input"/>
	                    </div>   
	    </div>
	    <div class="layui-form-item importNum" style="display:none;">
	    				<label class="layui-form-label" style="width: auto;opacity: 0;">&nbsp;&nbsp;&nbsp;投诉电话&nbsp;&nbsp;</label>
	                    <div class="layui-input-block" style="width: 250px;">
	                                <input type="text" name="importNum" id="importNum"
	                                            autocomplete="off" class="layui-input" disabled/>
	                    </div>   
	    </div>
	    <div class="layui-form-item import" style="display:none;">
	    						<label class="layui-form-label" style="width: auto;opacity: 0;">投诉电话导入</label>
                                <div class="layui-inline">
                                    <button type="button" class="layui-btn" id="import"><i class="layui-icon"></i>投诉电话导入</button>
                                </div>
                                <!-- <div class="layui-form-item">
                                	<label class="layui-form-label" style="width: auto;opacity: 0;">投诉电话导入</label>
                                    <span><a href="/smsBaseStation/apidownload"><u>基站导入帮助手册</u></a></span></br>
                                    <span><a href="/smsBaseStation/modeldownload"><u>基站导入模板下载</u></a></span>
                                </div> -->
        </div>
        <div class="layui-form-item import" style="display:none;">
        						<label class="layui-form-label" style="width: auto;opacity: 0;">投诉电话导入</label>
                                <div class="layui-inline">
                                    <button type="button" class="layui-btn" id="upload">导入
                                    </button>
                                </div>
        </div>
	    <div class="layui-block">
	        <button class="layui-btn layui-btn-normal" id="submit">提&nbsp;交</button>
	    </div>
    </div>
    </div>
</div>
</div>
</div>
</body>
</html>